package dao.Impl;

import dao.CommentDao;
import entity.Comment;
import handler.CommentListHandler;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import utils.C3p0Helper;

import java.sql.SQLException;
import java.util.List;

public class CommentDaoImpl implements CommentDao {
    @Override
    public List<Comment> findAll() {
        QueryRunner qr = new QueryRunner(C3p0Helper.getDataSource());
        String sql ="SELECT\n" +
                "\tc.id AS commentId,\n" +
                "\tc.content,\n" +
                "\tc.commentTime,\n" +
                "\tu.id AS userId,\n" +
                "\tu.userName AS userName,\n" +
                "\tu.pic AS pic,\n" +
                "\tp.id AS petId,\n" +
                "\tp.petName AS petName \n" +
                "\t\n" +
                "FROM\n" +
                "\tcomment c\n" +
                "\tJOIN user u ON c.userId = u.id\n" +
                "\tJOIN pet p ON c.petId = p.id;";
        ResultSetHandler<List<Comment>> handler = new CommentListHandler();
        try {
            return qr.query(sql,handler);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean update(Comment comment) {
        QueryRunner qr = new QueryRunner(C3p0Helper.getDataSource());
        String sql = "update comment set content=? where id=?";
        try {
            qr.update(sql,comment.getContent(),comment.getId());
            return true;
        } catch (Exception e) {
            return false;
        }

    }

    @Override
    public boolean delete(int id) {
        QueryRunner qr = new QueryRunner(C3p0Helper.getDataSource());
        String sql = "delete from comment where id=?";
        try {
            qr.update(sql,id);
            return true;
        } catch (Exception e) {

            return false;
        }

    }

    @Override
    public List<Comment> findByUserName(String userName) {
        QueryRunner qr = new QueryRunner(C3p0Helper.getDataSource());
        String sql = "SELECT \n" +
                "    c.id AS commentId,\n" +
                "    c.content,\n" +
                "    c.commentTime,\n" +
                "    u.id AS userId,\n" +
                "    u.userName AS userName,\n" +
                "    p.id AS petId,\n" +
                "    p.petName AS petName\n" +
                "FROM \n" +
                "    comment c\n" +
                "JOIN \n" +
                "    user u ON c.userId = u.id\n" +
                "JOIN \n" +
                "    pet p ON c.petId = p.id\n" +
                "WHERE \n" +
                "    u.userName = ?";
        ResultSetHandler<List<Comment>> handler = new CommentListHandler();
        try {
            return qr.query(sql, handler, userName);
        } catch (SQLException e) {
            e.printStackTrace(); // 打印堆栈跟踪
            throw new RuntimeException(e);
        }
    }
    @Override
    public void addComment(Comment comment) {
        QueryRunner qr = new QueryRunner(C3p0Helper.getDataSource());
        String sql = "INSERT INTO comment (content, commentTime, userId, petId) VALUES (?, ?, ?, ?)";
        try {
            System.out.println("添加评论内容: " + comment.getContent());
            System.out.println("评论时间: " + comment.getCommentTime());
            System.out.println("用户ID: " + comment.getUser().getId());
            System.out.println("宠物ID: " + comment.getPet().getId());
            qr.update(sql, comment.getContent(), comment.getCommentTime(), comment.getUser().getId(), comment.getPet().getId());
        } catch (Exception e) {
            e.printStackTrace(); // 打印堆栈跟踪
        }

    }

}
